package message

import (
	"car-thirdparty-api/internal/consts"
	"car-thirdparty-api/internal/dao"
	"car-thirdparty-api/internal/model"
	"car-thirdparty-api/internal/service"
	"car-thirdparty-api/internal/types"
	"context"
	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"

	"car-thirdparty-api/api/message/v1"
)

func (c *ControllerV1) MessagePlate(ctx context.Context, req *v1.MessagePlateReq) (res *v1.MessagePlateRes, err error) {
	// 查询车牌号对应的手机号
	phone := c.getPhone(ctx, req.Plate)
	if phone == "" {
		g.Log().Error(ctx, "根据车牌查询手机号失败")
		return nil, gerror.NewCode(gcode.CodeInternalError, "手机号为空")
	}
	err = g.Validator().Data(phone).Rules("phone-loose").Run(ctx)
	if err != nil {
		g.Log().Error(ctx, "手机号码格式不正确 ", phone)
		return nil, gerror.NewCode(gcode.CodeValidationFailed, "手机号码格式不正确")
	}

	_, err = service.MessageV1().SendMessage(ctx, &model.MessageSendReq{
		Content: req.Content,
		Phone:   phone,
		MsgType: consts.CS,
		Plate:   req.Plate,
	})
	if err != nil {
		g.Log().Error(ctx, "根据车牌发送消息失败：", err.Error())
		return nil, err
	}

	g.Log().Debug(ctx, "根据车牌发送消息成功")
	return
}

func (c *ControllerV1) getPhone(ctx context.Context, plate string) (phone string) {
	resData := c.queryAppCard(ctx, plate)
	if resData != nil {
		phone = resData.PhoneNum
		return
	}

	resData = c.queryAppCardOnline(ctx, plate)
	if resData != nil {
		phone = resData.PhoneNum
		return
	}

	resData = c.queryAppOrder(ctx, plate)
	if resData != nil {
		phone = resData.PhoneNum
		return
	}

	return
}

// 查询办证信息表
func (c *ControllerV1) queryAppCard(ctx context.Context, plate string) (resData *types.CarSearchRes) {
	var card *model.CardSearch

	err := dao.Card.Ctx(ctx).
		Where(dao.Card.Columns().CarNum, plate).
		WhereNotIn(dao.Card.Columns().Flag, []int{3, 13}).
		Limit(1).
		Scan(&card)
	if err != nil {
		g.Log().Error(ctx, "查询办证信息失败, ", err)
	}
	if card != nil {
		resData = &types.CarSearchRes{
			CarNum:   card.CarNum,
			DataType: consts.Card,
			Identity: card.CarId,
			Name:     card.CardName,
			PhoneNum: card.CardTel,
			JobId:    card.JobId,
		}
	}

	return
}

// 查询月租车信息表
func (c *ControllerV1) queryAppCardOnline(ctx context.Context, plate string) (resData *types.CarSearchRes) {
	var cardOnline *model.CardOnlineSearch
	err := dao.CardOnline.Ctx(ctx).
		Where(dao.CardOnline.Columns().CarNum, plate).
		Limit(1).
		Scan(&cardOnline)
	if err != nil {
		g.Log().Error(ctx, "查询月租车信息失败, ", err)
	}

	if cardOnline != nil {
		resData = &types.CarSearchRes{
			CarNum:   cardOnline.CarNum,
			DataType: consts.CardOnline,
			Identity: cardOnline.CardIdNum,
			Name:     cardOnline.CardName,
			PhoneNum: cardOnline.CardTel,
		}
	}
	return
}

// 查询预约车信息表
func (c *ControllerV1) queryAppOrder(ctx context.Context, plate string) (resData *types.CarSearchRes) {
	var bookingCard *model.BookingCardSearch
	err := dao.BookingCard.Ctx(ctx).
		Where(dao.BookingCard.Columns().CarNum, plate).
		OrderDesc(dao.BookingCard.Columns().CreateTime).
		Limit(1).
		Scan(&bookingCard)
	if err != nil {
		g.Log().Error(ctx, "查询预约车信息失败, ", err)
	}

	if bookingCard != nil {
		resData = &types.CarSearchRes{
			CarNum:   bookingCard.CarNum,
			DataType: consts.BookingCard,
			Identity: "",
			Name:     bookingCard.OrderUser,
			PhoneNum: bookingCard.OrderTel,
		}
	}

	return
}
